package com.cloneoner.video
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;


	/**
	 *  @author Thibault Loup - Cloneoner.com
	 */
	public class ClAbstractSlicePlayer extends Sprite
	{
		
		// __________________________________________________________
		// -										 	  CONSTRUCTOR
		
		/**
		 * Constructor
		 * @param pSlices The slices 
		 * @param pSmoothing Smoothing flag
		 */
		public function ClAbstractSlicePlayer(pSlices:Vector.<BitmapData>, pSmoothing:Boolean=false)
		{
			_slices = pSlices;
			_smoothing = pSmoothing;
			_totalFrames = pSlices.length;
			
			addChild(_display = new Bitmap(_slices[0]));
			if( _smoothing )_display.smoothing = true;
		}
		
		
		// __________________________________________________________
		// -										  PUBLICS METHODS
		
		/** The position of the frame in int */
		public function get currentFrame():int
		{
			return _currentFrame;
		}
		public function set currentFrame(pFrame:int):void
		{
			if ( _currentFrame == pFrame) return;

			var f:int = _normalizeFrame(pFrame);
			
			_display.bitmapData = _slices[f - 1];
			if( _smoothing )
				_display.smoothing = true;
			_currentFrame = f;
		}
		
		/** The totalframes of the slices in int */
		public function get totalFrames():int
		{
			return _totalFrames;
		}
		
		/** Smoothing */
		public function set smoothing(pFlag:Boolean):void
		{
			_smoothing = 
			_display.smoothing = pFlag;
		}
		
		/** Dispose */
		public function dispose():void
		{
			while (numChildren) removeChildAt(0);
			
			if( _slices)
			{
				var i:int;
				var ln:int = _slices.length;
				var b:BitmapData;
				
				while(i < ln)
				{
					b = _slices[i];
					if( b ) b.dispose();
					b = null;
					++i;
				}
			}
			
			_slices = null;
		}
		
		// __________________________________________________________
		// -										 PRIVATES METHODS
		
		/** @private */
		protected function _normalizeFrame(pValue:int):int
		{
			var f:int = pValue;
			var ln:int = _totalFrames;
			while ( f < 0 ) f += ln;
			f = ( (f == _totalFrames) || (f == 0) ) ? ln : f % ln;
			return f;
		}
		
		/* CONTENT */
		private var _display:Bitmap;
		
		/* VAR */
		private var _slices:Vector.<BitmapData>;
		private var _currentFrame:int;
		private var _smoothing:Boolean;
		private var _totalFrames:int;
	}
}
